import Dep from './Dep';
import observe from './observe';

export default function defineReactive (data, key, value) {
    const dep = new Dep();
    if (arguments.length === 2) {
        value = data[key];
    }

    observe(value);
    // let childOb = observe(value);

    Object.defineProperty(data, key, {
        // 可枚举 可以for-in
        enumerable: true,
        // 可被配置，比如可以被delete
        configurable: true,
        get () {
            // debugger;
            console.log('get');
            if (Dep.target) {
                dep.depend();
                // if (childOb) {
                //     childOb.dep.depend();
                // }
            }
            return value;
        },
        set (newValue) {
            // console.log(newValue);
            if (value === newValue) return;
            value = newValue;
            // childOb = observe(newValue);
            dep.notify();
        }
    });
};
